{include file="public/layout" /}
<body class="laytp-container">
<div class="layui-card" id="search-form">
	<div class="layui-card-body">
		<div class="layui-card-header">
			<i class="fa fa-pencil"></i> 编辑权限
		</div>
		<form class="layui-form" action="">
			<input type="hidden" name="role_id" value="{$detail.role_id}" />
			<div class="layui-form-item">
				<label class="layui-form-label">角色名称</label>
				<div class="layui-input-inline layui-input-wrap">
					<input type="text" name="role_name" value="{$detail.role_name}" placeholder="角色名称" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">角色描述</label>
				<div class="layui-input-inline layui-input-wrap">
					<input type="text" name="role_desc" value="{$detail.role_desc}" placeholder="角色描述" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">权限</label>
				<div class="layui-input-inline layui-input-wrap">
					<input type="radio" name="role_type" value="0" {if empty($detail) || $detail['role_type']==0}checked{/if} title="全部" class="layui-input" lay-filter="role_type">
					<input type="radio" name="role_type" value="1" {if !empty($detail) && $detail['role_type']==1}checked{/if} title="选择" class="layui-input" lay-filter="role_type">
				</div>
			</div>
			<div class="layui-form-item role_menu"  {if empty($detail) || $detail['role_type']==0}style="display:none;"{/if} >
				<label class="layui-form-label">权限分配</label>
				<div class="layui-input-block">
					<div style="margin-top:10px;color:#303030; font-size:14px; font-weight:600; ">
						<input type="checkbox" title="全部选择" lay-skin="primary" lay-filter="checkall_all"/>
					</div>
					{foreach $modules as $kk=>$menu}
						<div>
							<div style="clear:left;margin-top:10px;color:#303030; font-size:14px; font-weight:600; ">
								<input type="checkbox" title="{$group[$kk]}" value="" lay-skin="primary" lay-filter="checkall"/>
							</div>
							<div style="margin-left:20px">
								{foreach $menu as $menu2}
									<div>
										<div style="width: 100px;float:left;margin-top:8px;clear:left">
											<input class="checkbox" title="{$menu2['name']}" type="checkbox" lay-skin="primary" lay-filter="checkall" />
										</div>
										<div style="clear:both;float: left;"></div>
										{foreach $menu2['child'] as $vv}
											<div style="min-width: 120px;float: left;">
												<input class="checkbox" name="right[]" value="{$vv.id}" title="{$vv.name}" {if $vv['enable'] == 1}checked{/if} ck="mod-{$kk}" type="checkbox" />
											</div>
										{/foreach}
										<div style="clear:both;float: left;"></div>
									</div>
								{/foreach}
							</div>
							<div style="clear:both;float: left;margin-bottom:10px"></div>
						</div>
					{/foreach}
				</div>
			</div>

			<div class="layui-form-item">
				<div class="layui-input-block">
					<button type="submit" class="layui-btn" lay-submit lay-filter="demo1">确认</button>
				</div>
			</div>
		</form>
	</div>
</div>
</body>

<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script>
	layui.use(['form', 'laydate', 'util'], function(){
		var form = layui.form;
		var layer = layui.layer;
		var laydate = layui.laydate;
		var util = layui.util;

		// 自定义验证规则
		form.verify({
			pass: [
				/^[\S]{6,12}$/,
				'密码必须6到12位，且不能出现空格'
			]
		});

		// 指定开关事件
		// form.on('switch(switchTest)', function(data){
		//     layer.msg('开关 checked：'+ (this.checked ? 'true' : 'false'), {
		//         offset: '6px'
		//     });
		//     layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是 ON|OFF', data.othis)
		// });

		// 提交事件
		form.on('submit(demo1)', function(data){
			var index = layer.load();
			layer.close(index);

			var field = data.field; // 获取表单字段值
			$.ajax({
				type: "POST",
				url: "{:url('roleSave')}",
				data: field,
				success: function(res){
					console.log(res);
					if(res.status){
						layer.msg('提交成功');
					}else{
						layer.msg(res.msg);
					}
					setTimeout(function(){
						parent.layer.closeAll();
						parent.tableIns.reload()
					},1000)
				}
			});
			return false; // 阻止默认 form 跳转
		});

		// 日期
		laydate.render({
			elem: '#date'
		});
		layui.form.on('checkbox(checkall_all)',function(data){
			if(data.elem.checked){
				$(data.elem).parent().parent().find('input[type=checkbox]').prop('checked',true);
			}else{
				$(data.elem).parent().parent().find('input[type=checkbox]').prop('checked',false);
			}
			layui.form.render('checkbox');
		})
		layui.form.on('checkbox(checkall)',function(data){
			if(data.elem.checked){
				$(data.elem).parent().parent().find('input[type=checkbox]').prop('checked',true);
			}else{
				$(data.elem).parent().parent().find('input[type=checkbox]').prop('checked',false);
			}
			layui.form.render('checkbox');
		})
		layui.form.on('radio(role_type)', function (data) {
			var value = data.value; // 获得被选中的值
			if(value==1){
				$('.role_menu').show();
			}else{
				$('.role_menu').hide();
			}
		});

	});
</script>
</body>
</html>